package com.wuuxiang.redis.spring.boot.autoconfigure;

import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration    //@Configuration标识此类为一个spring配置类
@ComponentScan
public class RedisAutoConfiguration {

	@Bean
	// @ConfigurationProperties(prefix="spring.redis")
	// 这里切记要用?,?,用Object报错
	public RedisTemplate<?, ?> redisTemplate(JedisConnectionFactory jedisConnectionFactory) {
		System.out.println("jedisConnectionFactory.getHostName():" + jedisConnectionFactory.getHostName());
		System.out.println("jedisConnectionFactory.getPassword():" + jedisConnectionFactory.getPassword());
		System.out.println("jedisConnectionFactory.getDatabase():" + jedisConnectionFactory.getDatabase());
		System.out.println("jedisConnectionFactory.getPort():" + jedisConnectionFactory.getPort());
		System.out.println("jedisConnectionFactory.getPoolConfig().getMaxIdle():" + jedisConnectionFactory.getPoolConfig().getMaxIdle());
        RedisTemplate<?, ?> template = new RedisTemplate<Object, Object>();
        template.setConnectionFactory(jedisConnectionFactory); //必须赋值
        template.setKeySerializer(new StringRedisSerializer());
        // template.setValueSerializer(new RedisObjectSerializer());
        template.setEnableDefaultSerializer(true);
        return template;
    }

    public static void main(String[] args) {
		SpringApplication.run(RedisAutoConfiguration.class, args);
	}
}
